package com.lanhe.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.member.domain.LhStatYearIncome;
import com.lanhe.member.mapper.LhStatYearIncomeMapper;
import com.lanhe.member.service.ILhStatYearIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * (LhStatYearIncome)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-11 09:58:47
 */
@Service
public class LhStatYearIncomeServiceImpl extends ServiceImpl<LhStatYearIncomeMapper, LhStatYearIncome> implements ILhStatYearIncomeService {

	@Autowired
	private LhStatYearIncomeMapper lhStatYearIncomeMapper;

	/**
	 * 增加
	 *
	 * @param statYearIncome 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public int insertStatYearIncome(LhStatYearIncome statYearIncome) {

		LambdaQueryWrapper<LhStatYearIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatYearIncome::getMemberId, statYearIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatYearIncome::getYear, statYearIncome.getYear());
		LhStatYearIncome statYear = lhStatYearIncomeMapper.selectOne(lambdaQueryWrapper);
		if (ObjectUtil.isNotNull(statYear)) {

			statYearIncome.setUpdateTime(new Date());
			statYearIncome.setAmount(statYear.getAmount().add(statYearIncome.getAmount()));
			return lhStatYearIncomeMapper.update(statYearIncome,lambdaQueryWrapper);
		} else {

			statYearIncome.setCreateTime(new Date());
			return lhStatYearIncomeMapper.insert(statYearIncome);
		}
	}


	/**
	 * 列表
	 *
	 * @param pageNum  页码
	 * @param pageSize 条数
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public List<LhStatYearIncome> rankingList(Integer pageNum, Integer pageSize) {

		// 获取当前日期
		LocalDate currentDate = LocalDate.now();

		LambdaQueryWrapper<LhStatYearIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatYearIncome::getYear,currentDate.getYear());
		lambdaQueryWrapper.orderByDesc(LhStatYearIncome::getAmount);
		return lhStatYearIncomeMapper.selectList(lambdaQueryWrapper);
	}

	@Override
	public LhStatYearIncome getInfo(LhStatYearIncome statYearIncome) {

		LambdaQueryWrapper<LhStatYearIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatYearIncome::getMemberId, statYearIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatYearIncome::getYear, statYearIncome.getYear());
		return lhStatYearIncomeMapper.selectOne(lambdaQueryWrapper);
	}
}

